package net.simflow.swf.analyzer.gui.ginfo.display;

import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class GraphInfoDisplayConfigComposite extends Composite {

	private Table tblMain;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public GraphInfoDisplayConfigComposite(Composite parent,
			LinkedHashMap<String, Boolean> map) {
		super(parent, SWT.NONE);

		this.setLayout(new GridLayout(1, false));

		Composite cmpButton = new Composite(this, SWT.NONE);
		GridLayout gl_cmpButton = new GridLayout(6, false);
		gl_cmpButton.marginWidth = 0;
		gl_cmpButton.marginHeight = 0;
		cmpButton.setLayout(gl_cmpButton);
		cmpButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1,
				1));

		Button btnSelectAll = new Button(cmpButton, SWT.NONE);
		btnSelectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnSelectAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for (TableItem item : tblMain.getItems()) {
					item.setChecked(true);
				}
			}
		});
		btnSelectAll.setText("All");

		Button btnDeselectAll = new Button(cmpButton, SWT.NONE);
		btnDeselectAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnDeselectAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for (TableItem item : tblMain.getItems()) {
					item.setChecked(false);
				}
			}
		});
		btnDeselectAll.setText("Clear");

		Button btnSelectInvert = new Button(cmpButton, SWT.NONE);
		btnSelectInvert.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnSelectInvert.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for (TableItem item : tblMain.getItems()) {
					item.setChecked(!item.getChecked());
				}
			}
		});
		btnSelectInvert.setText("Invert");

		Label lblBlank = new Label(cmpButton, SWT.NONE);
		lblBlank.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Button btnUp = new Button(cmpButton, SWT.ARROW | SWT.UP);
		btnUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				changeOrderInTable(tblMain, false);
			}
		});
		btnUp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));

		Button btnDown = new Button(cmpButton, SWT.ARROW | SWT.DOWN);
		btnDown.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				changeOrderInTable(tblMain, true);
			}
		});
		btnDown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));

		tblMain = new Table(this, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		tblMain.setLinesVisible(true);
		tblMain.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		if (map == null) {
			return;
		}

		for (Entry<String, Boolean> en : map.entrySet()) {
			TableItem item = new TableItem(tblMain, SWT.NONE);
			item.setText(en.getKey());
			item.setChecked(en.getValue());
		}
	}

	private void changeOrderInTable(Table table, boolean isDown) {
		int s = table.getSelectionIndex();
		if (s < 0 || s >= table.getItemCount()) {
			return;
		}
		if (s == 0 && !isDown) {
			return;
		}
		if (s == table.getItemCount() - 1 && isDown) {
			return;
		}

		int ex = (isDown) ? 2 : -1;
		TableItem item = table.getItem(s);
		TableItem newItem = new TableItem(table, SWT.NONE, s + ex);
		newItem.setText(item.getText());
		newItem.setChecked(item.getChecked());
		int r = (isDown) ? s : s + 1;
		table.remove(r);
		int ns = (isDown) ? s + 1 : s - 1;
		table.setSelection(ns);
	}

	protected LinkedHashMap<String, Boolean> getResult() {
		LinkedHashMap<String, Boolean> map = new LinkedHashMap<String, Boolean>();

		for (int i = 0; i < tblMain.getItemCount(); i++) {
			map.put(tblMain.getItem(i).getText(), tblMain.getItem(i)
					.getChecked());
		}
		return map;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
